package data.bot;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.List;

import config.Constants;

import main.WoTCWBot;
import timertasks.BattleReminderTimerTask;
import utils.Utils;
import data.wg.AbstractWoTCWBotListEntity;
import data.wg.WGCombat;
import data.wg.WGCombatant;
import data.wg.WGScheduledBattle;

public class CombatContainer extends AbstractBattleContainer
{
	
	public WGCombat wgcombat;
	public String owner;

	public CombatContainer(WGCombat c)
	{
		wgcombat=c;
		idOfContent = c.id;
		
	}


	
	
	@Override
	public String getString(StringMode sm)
	{
		String enemy = "defaultenemy";

		
		switch(sm)
		{
		case CombatList:
			String minutes;
			Calendar atCalendar = Calendar.getInstance();
			atCalendar.setTimeInMillis(time*1000);


			
			int min = atCalendar.get(Calendar.MINUTE);
			if(min<10)
			{
				minutes = "0"+min;
			}
			else
			{
				minutes=min+"";
			}
			
			if(isFinal)
			{
				return atCalendar.get(Calendar.HOUR_OF_DAY)+":"+minutes+" "+wgcombat.ARENA+" ("+wgcombat.PROVINCE+", "+enemy+")";
			}
			return atCalendar.get(Calendar.HOUR_OF_DAY)+":"+minutes+"+ "+wgcombat.ARENA+" ("+wgcombat.PROVINCE+", "+enemy+")";
			
		case BattleReminderTaskString:
			return wgcombat.ARENA+" ("+wgcombat.PROVINCE+", "+enemy+")";
		}

		return "Invalid StringMode";
	}
	
	
	
	
	/**
	 *  checks the time with the schedule, creates the timers and adds the container to it's list.
	 * @param tempCombatContainer
	 * @param tempWGCombatant
	 */
	public void calculateTimeAndFinalityOfCombat(WGCombatant ownClan, String owner, long primetime)
	{
		isFinal = ownClan.at!=null;
		if(wgcombat.type.equals("for_province"))
		{
			if(!isFinal)
			{
				if(owner.equals(Constants.CLAN_NAME))
				{
					time =primetime + 60*60 + 60*30*(wgcombat.combatants.size()-1);  //60min offset + combs-1 * 30
					WoTCWBot.LOGGER.trace("(BfP) (own province) unfinal time set to "+Utils.getDateString(time));
				}
				else
				{
					
					time = primetime + 60*90;
					WoTCWBot.LOGGER.trace("(BfP) (foreign province) unfinal time set to "+Utils.getDateString(time));
				}
			}
			else
			{
				Collections.sort(wgcombat.combatants);
				
				int counter=0;
				for(int i=0;i<wgcombat.combatants.size();i++)
				{
					WGCombatant wg = wgcombat.combatants.get(i);
					if(wg.name.equals(Constants.CLAN_NAME))
					{
						counter=i-1;
					}
				}
				if(counter<0)
				{
					counter=0;
				}
				WoTCWBot.LOGGER.trace("BfPTreecounter="+counter);
				long firstfighttime = wgcombat.combatants.get(0).at;
				long earliestEstimatedTime = firstfighttime + 60*30*counter;
				
				WGScheduledBattle scheduled = WoTCWBot.findScheduledBattleByName(wgcombat.PROVINCE);
				if(scheduled!=null)
				{
					long scheduletime = scheduled.at;
					time = Math.max(earliestEstimatedTime, scheduletime);	
				}
				else
				{
					WoTCWBot.LOGGER.warn("no scheduledbattle found!");
					time = earliestEstimatedTime;
				}
				
				WoTCWBot.LOGGER.trace("final time set to "+Utils.getDateString(time));
			}
		}
		else if(wgcombat.type.equals("meeting_engagement"))
		{
			if(!isFinal)
			{
				time = primetime;
				WoTCWBot.LOGGER.trace("(encounter) unfinal time set to "+Utils.getDateString(time));
			}
			else
			{
				time = ownClan.at;
				WoTCWBot.LOGGER.trace("(encounter) final time set to "+Utils.getDateString(time));
			}
		}
		else
		{
			WoTCWBot.LOGGER.error("UNKNOWN BATTLE TYPE!");
		}
	}

	
	
}
